package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.data.ContactData;

public class ContactDataGenerator {
	
	Random rnd = new Random();
	
	@DataProvider(name = "contacts") 
	public static Iterator<Object[]> contacts() {
		List<Object[]> list=  new ArrayList<Object[]>();
		int numberOfContactsInData = 1;
		List<ContactData> contacts=  new ContactDataGenerator().generateRandomContactList(numberOfContactsInData);
		for (ContactData contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile") 
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list=  new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			// generateDataToFile method
			ContactData contact = new ContactData().setFirstName(parts[0])
					.setLastName(parts[1]).setEmail1(parts[2]).setEmail2(parts[3])
					.setDay(parts[4]).setMonth(parts[5]).setYear(parts[6])
					.setHomeTel(parts[7]).setHome2(parts[8]).setMobileTel(parts[9])
					.setWorkTel(parts[10]).setAddress(parts[11]).setAddress2(parts[12]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}
	public static void main(String[] args) {
		if (args.length <2) {
			System.out.println("Error: specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactData> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()) {
			System.out.println("File exists. Stopping generator");
			return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.firstName + "\t" + contact.lastName + "\t"  +
					contact.email1 + "\t" + contact.email2 + "\t"  +
					contact.day + "\t" + contact.month + "\t" + contact.year + "\t" +
					contact.homeTel + "\t" + contact.home2 + "\t" + 
					contact.mobileTel + "\t" + contact.workTel + "\t" + 
					contact.address + "\t" + contact.address2 + "\t" + "\n");
			writer.flush();
		}
		writer.close();
	}

	private List<ContactData> generateRandomContactList(int numberOfContactsInData) {
		List<ContactData> list=  new ArrayList<ContactData>();
		String[] monthsSet = {"January", "February", "March", "April", "May", "June", 
				"July", "August", "September", "October", "November", "December"};
		int numOfDigits = 4;
		for (int i = 0; i < numberOfContactsInData; i++) {
			String month = Arrays.asList(monthsSet).get(rnd.nextInt(monthsSet.length)); 
			ContactData contact = new ContactData()
			.setAddress("Address " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setAddress2("Address2 " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setDay(Integer.toString(1 + rnd.nextInt(31)))
			.setEmail1("Email1 " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setEmail2("Email2 " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setFirstName("FirstName " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setHome2("Home2 " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setHomeTel("HomeTel " + rnd.nextInt(999999999))
			.setLastName("LastName " + TestBase.generateAlphaNumbericRandom(numOfDigits))
			.setMobileTel("MobileTel " + rnd.nextInt(999999999))
			.setMonth(month)
			.setWorkTel("WorkTel " + rnd.nextInt(999999999))
			.setYear(Integer.toString( rnd.nextInt(Calendar.getInstance().get(Calendar.YEAR) + 1)));
			list.add(contact);
		}
		return list;
	}
}
